/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.latestrptdate;

import com.google.common.collect.ImmutableMap;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2023/9/13 20:54 
 * @since JDK 1.8
 */
public class UpdateLatestDate {


    static Map<String, String> dataSourceSchemaMap = new HashMap() {{
        put("yto_ytrpt86", "ytrpt");
        put("yto_bigdata_crm", "bigdata_crm");
        put("yto_bigdata_finance", "bigdata_finance");
        put("yto_bigdata_mutiple", "bigdata_mutiple");
        put("yto_bigdata_doris", "ytrpt");
        put("yto_ytrpt", "ytrpt");
        put("yto_bigdata_platform", "ytrpt");
        put("yto_bigdata_operate", "bigdata_operate");
        put("yto_bigdata_app", "bigdata_app");
    }};


    static Map<String, Map<String, String>> dataSourceInfoMap = new HashMap() {{
        put("yto_ytrpt86", ImmutableMap.of("driverClassName", "oracle.jdbc.driver.OracleDriver", "url", "jdbc:oracle:thin:@10.130.9.230:1521:bidbuat", "username", "ytrpt", "password", "PUwA995A"));
        put("yto_bigdata_crm", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/bigdata_crm?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
        put("yto_bigdata_finance", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/bigdata_finance?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
        put("yto_bigdata_mutiple", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/bigdata_mutiple?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
        put("yto_bigdata_doris", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.66:9030/ytrpt?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "null"));
        put("yto_ytrpt", ImmutableMap.of("driverClassName", "oracle.jdbc.driver.OracleDriver", "url", "jdbc:oracle:thin:@10.130.9.230:1521:bidbuat", "username", "ytrpt", "password", "PUwA995A"));
        put("yto_bigdata_platform", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.11.217:4000/ytrpt?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "null"));
        put("yto_bigdata_operate", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/bigdata_operate?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
        put("yto_bigdata_app", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/bigdata_app?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
        put("yto_jspapp_mysql", ImmutableMap.of("driverClassName", "com.mysql.jdbc.Driver", "url", "jdbc:mysql://10.130.253.67:3306/jscapp?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "username", "root", "password", "0JZ^YQHxjII!p1Qf"));
    }};

    static String ds_yto_ytrpt_url = "jdbc:oracle:thin:@10.130.9.230:1521:bidbuat";
    static String ds_yto_ytrpt_username = "ytrpt";
    static String ds_yto_ytrpt_password = "PUwA995A";


    private static java.util.Date getLatestRptDate(Map<String, Object> paramMap) {
        Map<String, String> dataSourceMap = dataSourceInfoMap.get(paramMap.get("TABLE_DATASOURCE"));
        try {
            Class.forName(dataSourceMap.get("driverClassName"));
            Connection connection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), "null".equals(dataSourceMap.get("password")) ? null : dataSourceMap.get("password"));
            PreparedStatement ps = null;
            ResultSet rs = null;
            StringBuilder sqlBuilder = new StringBuilder("select max(rpt_date) rpt_date from " + paramMap.get("TABLE_NAME"));
            if ("Y".equals(paramMap.get("IS_SAME_TABLE"))) {
                sqlBuilder.append(" where sum_type = ?");
                ps = connection.prepareStatement(sqlBuilder.toString());
                ps.setString(1, (String) paramMap.get("SUM_TYPE"));
            } else {
                ps = connection.prepareStatement(sqlBuilder.toString());
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                Date date = rs.getDate("rpt_date");
                return date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }

    private static void doDealWithRptDate(Map<String, Object> paramMap) {
        String remarkFlag = (String) paramMap.get("REMAKE_FLAG");
        //获取最新日期
        java.util.Date latestRptDate = getLatestRptDate(paramMap);
        if (null == latestRptDate) {
            System.out.println("当前日期配置表维度：" + paramMap.get("SUM_TYPE") + "下无最新日期!");
            return;
        }
        if ("1".equals(remarkFlag)) {
            Map<String, String> dataSourceMap = dataSourceInfoMap.get("yto_jspapp_mysql");
            try {
                Class.forName(dataSourceMap.get("driverClassName"));
                Connection connection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                StringBuilder sqlBuilder = new StringBuilder("select * from jscapp.result_table_rpt_date_info where table_name= ? and sum_type = ?");
                PreparedStatement ps = connection.prepareStatement(sqlBuilder.toString());
                ps.setString(1, (String) paramMap.get("TABLE_NAME"));
                ps.setString(2, (String) paramMap.get("SUM_TYPE"));
                ResultSet rs = ps.executeQuery();
                long time = new java.util.Date().getTime();
                if (rs.next()) {
                    Connection updateConnection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                    StringBuilder updateSqlBuilder = new StringBuilder("update jscapp.result_table_rpt_date_info set last_rpt_date = rpt_date ,rpt_date = ?,update_date=?,update_time=? where table_name = ? and sum_type = ?");
                    PreparedStatement updatePs = updateConnection.prepareStatement(updateSqlBuilder.toString());
                    updatePs.setDate(1, new Date(latestRptDate.getTime()));
                    updatePs.setDate(2, new Date(time));
                    updatePs.setDate(3, new Date(time));
                    updatePs.setString(4, (String) paramMap.get("TABLE_NAME"));
                    updatePs.setString(5, (String) paramMap.get("SUM_TYPE"));
                    updatePs.executeUpdate();
                } else {
                    Connection insertConnection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                    StringBuilder insertSqlBuilder = new StringBuilder("insert into jscapp.result_table_rpt_date_info(table_name,table_db,rpt_date,sum_type,system_flag,create_time,create_date,create_operator)" + " values(?,?,?,?,?,?,?,?)");
                    PreparedStatement insertPs = insertConnection.prepareStatement(insertSqlBuilder.toString());
                    insertPs.execute(insertSqlBuilder.toString());
                    insertPs.setString(1, (String) paramMap.get("TABLE_NAME"));
                    insertPs.setString(2, (String) paramMap.get("TABLE_DATASOURCE"));
                    insertPs.setDate(3, new Date(latestRptDate.getTime()));
                    insertPs.setString(4, (String) paramMap.get("SUM_TYPE"));
                    insertPs.setString(5, "1");
                    insertPs.setDate(6, new Date(time));
                    insertPs.setDate(7, new Date(time));
                    insertPs.setString(8, "01990709");
                    insertPs.execute();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        } else {
            Map<String, String> dataSourceMap = dataSourceInfoMap.get(paramMap.get("TABLE_DATASOURCE"));
            try {
                Class.forName(dataSourceMap.get("driverClassName"));
                Connection connection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                StringBuilder sqlBuilder = new StringBuilder("select * from jsc_app_prt_date where busi_table_name= ? and sum_type = ?");
                PreparedStatement ps = connection.prepareStatement(sqlBuilder.toString());
                ps.setString(1, (String) paramMap.get("TABLE_NAME"));
                ps.setString(2, (String) paramMap.get("SUM_TYPE"));
                ResultSet rs = ps.executeQuery();
                java.util.Date currentDate = new java.util.Date();
                if (rs.next()) {
                    Connection updateConnection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                    StringBuilder updateSqlBuilder = new StringBuilder("update jsc_app_prt_date set last_rpt_date = rpt_date,rpt_date = ?,update_time=? where busi_table_name = ? and sum_type = ?");
                    PreparedStatement updatePs = updateConnection.prepareStatement(updateSqlBuilder.toString());
                    updatePs.setDate(1, new Date(latestRptDate.getTime()));
                    updatePs.setTimestamp(2, new Timestamp(currentDate.getTime()));
                    updatePs.setString(3, (String) paramMap.get("TABLE_NAME"));
                    updatePs.setString(4, (String) paramMap.get("SUM_TYPE"));
                    updatePs.executeUpdate();
                } else {
                    Connection insertConnection = DriverManager.getConnection(dataSourceMap.get("url"), dataSourceMap.get("username"), dataSourceMap.get("password"));
                    StringBuilder insertSqlBuilder = new StringBuilder("insert into jsc_app_prt_date(busi_table_name,rpt_date,sum_type,create_time,create_operator)" + " values(?,?,?,?,?)");
                    PreparedStatement insertPs = insertConnection.prepareStatement(insertSqlBuilder.toString());
                    insertPs.execute(insertSqlBuilder.toString());
                    insertPs.setString(1, (String) paramMap.get("TABLE_NAME"));
                    insertPs.setDate(2, new Date(latestRptDate.getTime()));
                    insertPs.setString(3, (String) paramMap.get("SUM_TYPE"));
                    insertPs.setDate(4, new Date(currentDate.getTime()));
                    insertPs.setString(5, "01990709");
                    insertPs.execute();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        }
    }


    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection = DriverManager.getConnection(ds_yto_ytrpt_url, ds_yto_ytrpt_username, ds_yto_ytrpt_password);
        String tableType = "storageVolume";
        String sql = "select * from ytrpt.T_TABLE_LASTEST_DATE_MAP where table_type = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, tableType);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("TABLE_NAME", rs.getObject("TABLE_NAME"));
            paramMap.put("TABLE_DESC", rs.getObject("TABLE_DESC"));
            paramMap.put("TABLE_TYPE", rs.getObject("TABLE_TYPE"));
            paramMap.put("DATE_COLUM_NAME", rs.getObject("DATE_COLUM_NAME"));
            paramMap.put("SUM_TYPE", rs.getObject("SUM_TYPE"));
            paramMap.put("IS_SAME_TABLE", rs.getObject("IS_SAME_TABLE"));
            paramMap.put("TABLE_DATASOURCE", rs.getObject("TABLE_DATASOURCE"));
            paramMap.put("REMAKE_FLAG", rs.getObject("REMAKE_FLAG"));
            doDealWithRptDate(paramMap);
        }
    }
}